package classtest;

public class Test5 {
    public static void main(String[] args) {
        //高效拼接大量字符串
        //需求：假设有一个包含10000个随机字符串的列表，分别用String和StringBuffer拼接，并计算耗时
        //提示使用System.currentTimeMillis()
        stringTest1();
        stringBufferTest();
        stringBuilderTest();
    }

    private static void stringBuilderTest() {
        long start2 = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 1000000; i++) {
            sb.append(i);
        }
        long end2 = System.currentTimeMillis();
        System.out.println("StringBuilder拼接耗时："+(end2-start2));
    }
    private static void stringBufferTest() {
        long start1 = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 1000000; i++) {
            sb.append(i);
        }
        long end1 = System.currentTimeMillis();
        System.out.println("StringBuffer拼接耗时："+(end1-start1));
    }

    private static void stringTest1() {
        long start = System.currentTimeMillis();
        String s = "";
        for (int i = 0; i < 10000; i++) {
            s += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("String拼接耗时："+(end-start));
    }
}
